﻿using System;
using System.Windows.Input;
using ClientOverflow.Core;

namespace ClientOverflow.ViewModels.PostQuestion
{
    /// <summary>
    /// SaveQuestionCommand is in charge of posting the question by sending the model, which relies in the back
    /// of the IQuestionViewModel, to the DB through the WCF serivce.
    /// </summary>
    internal class SaveQuestionCommand : ICommand
    {
        private readonly DataTransferObjects.Question question;

        public SaveQuestionCommand(DataTransferObjects.Question question)
        {
            this.question = question;
        }

        public void Execute(object parameter)
        {
            var loggenInUser = ServicesProviderFactory.GetInstance().LoginService.LoggenInUser;

            using (var questionsService = ServicesFactory.CreateClientOverflowServiceClient())
            {
                questionsService.RegisterNewQuestion(question, loggenInUser);
            }
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;
    }
}